package com.example.democlient.network.packet;


import java.nio.ByteBuffer;

import com.example.democlient.network.ServerConnection;


/**
 * @author ATracer
 */
public abstract class ServerPacket extends BaseServerPacket
{
	/**
	 * constructs new server packet with specified opcode.
	 * 
	 * @param opcode
	 *            packet id
	 */
	protected ServerPacket(int opcode)
	{
		super(opcode);
	}

	/**
	 * Write this packet data for given connection, to given buffer.
	 * 
	 * @param con
	 * @param buf
	 */
	public final void write(ServerConnection con, ByteBuffer buf)
	{
		buf.putShort((short) 0);
		writeC(buf,getOpcode());
		writeImpl(con, buf);
		buf.flip();
		buf.putShort((short) buf.limit());
		buf.position(0);
	}

	/**
	 * Write data that this packet represents to given byte buffer.
	 * 
	 * @param con
	 * @param buf
	 */
	protected abstract void writeImpl(ServerConnection con, ByteBuffer buf);
}
